package product;

import jodd.bean.BeanCopy;
import models.BaseModel;
import models.constants.DeletedStatus;
import play.modules.paginate.JPAExtPaginator;

import javax.persistence.*;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/10/12.
 */
@Entity
@Table(name = "company")
public class Company extends BaseModel {



    //公司名称
    @Column(name="company_name")
    public String companyName;

    //捐赠内容
    @Lob
    @Column(name="content")
    public String content;



    //逻辑删除
    @Column(name="deleted")
    @Enumerated(EnumType.ORDINAL)
    public DeletedStatus deleted;

    @Transient
    public String createAtStr;


    /**
     * 分页查询.
     */
    public static JPAExtPaginator<Company> findByCondition(Map<String, Object> conditionMap, String orderByExpress, int pageNumber, int pageSize) {
        StringBuilder xsqlBuilder = new StringBuilder("t.deleted=models.constants.DeletedStatus.UN_DELETED ")
                .append("/~ and t.companyName like {name} ~/");
//                .append("/~ and t.merchant.id = {merchantId} ~/");

        util.xsql.XsqlBuilder.XsqlFilterResult result = new util.xsql.XsqlBuilder().generateHql(xsqlBuilder.toString(), conditionMap);
        JPAExtPaginator<Company> orderPage = new JPAExtPaginator<Company>("Company t", "t", Company.class,
                result.getXsql(), conditionMap).orderBy(orderByExpress);
        orderPage.setPageNumber(pageNumber);
        orderPage.setPageSize(pageSize);
        orderPage.setBoundaryControlsEnabled(false);
        return orderPage;
    }
    /**
     * 根据id查询
     */
    public static Company findById(Long id){
        return Company.find("deleted = ? and id =?",DeletedStatus.UN_DELETED,id).first();
    }
    /**
     * Update
     *
     * @param id
     * @param newObject
     */
    public static void update(Long id, Company newObject) {
        Company company = Company.findById(id);
        BeanCopy.beans(newObject, company).ignoreNulls(true).copy();
        company.save();
    }
    //查询全部
    public static List<Company> findAllCompany(){
        return Company.find("deleted = 0 order by createAt desc ").fetch();
    }
}
